package com.ynet.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.ynet.http.ForReq;
import com.ynet.utils.PropertiesUtils;

public class Main extends JFrame implements ActionListener{
	
	private static final long serialVersionUID = 1L;
	
	private JPanel contentPane1;
	private JPanel contentPane2;
	private JPanel contentPane3;
	private JPanel contentPane4;
	private JPanel contentPane5;
	private JPanel contentPane6;

	private JLabel jl1;//用户名
	private JLabel jl2;//密码
	private JLabel jl3;//内网地址
	private JLabel jl4;//外网地址
	
	private JTextField userName;//用户名
	private JTextField password;//密码
	private JTextArea innerUrl;
	private JTextArea wanUrl;//
	
	private JButton jb1;//登陆
	private JButton jb2;//注册
	
	private static String uname;
	private static String pw;
	private JPanel panel_1;
	private JLabel label_1;
	private JLabel lblNewLabel;
	
	public static void start(String userName, String password){
		
		Main.uname = userName;
		Main.pw = password;
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main frame = new Main();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	
	}
	
	public static void main(String[] args) {
		start("", "");
	}

	public Main() {

		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.NORTH);

		ImageIcon ico = new ImageIcon("bananer.png");
		ico.setImage(ico.getImage().getScaledInstance(130, 50, Image.SCALE_DEFAULT));

		JLabel label = new JLabel(ico);
		panel.add(label);
		label.setFont(new Font("宋体", Font.PLAIN, 35));
		
		setTitle("\u62A5\u6587\u8F6C\u53D1");
		setBackground(Color.WHITE);
		contentPane1 = new JPanel();
		FlowLayout flowLayout = (FlowLayout) contentPane1.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		contentPane2 = new JPanel();
		FlowLayout flowLayout_1 = (FlowLayout) contentPane2.getLayout();
		flowLayout_1.setAlignment(FlowLayout.LEFT);
		contentPane3 = new JPanel();
		FlowLayout flowLayout_2 = (FlowLayout) contentPane3.getLayout();
		flowLayout_2.setAlignment(FlowLayout.LEFT);
		contentPane4 = new JPanel();
		FlowLayout flowLayout_3 = (FlowLayout) contentPane4.getLayout();
		flowLayout_3.setAlignment(FlowLayout.LEFT);
		contentPane5 = new JPanel();
		contentPane6 = new JPanel();
		
		jl1 = new JLabel("用户名：");
		jl2 = new JLabel("密    码：");
		jl3 = new JLabel("内网地址：");
		jl4 = new JLabel("服务器地址：");
		
		userName = new JTextField();
		userName.setText(Main.uname);
		userName.setColumns(15);
		password = new JTextField();
		password.setText(Main.pw);
		password.setColumns(15);

		jb1 = new JButton("\u5F00\u59CB\u94FE\u63A5");
		jb1.addActionListener(this);
		jb2 = new JButton("退出");
		jb2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(-1);
			}
		});
		
		contentPane1.add(jl1);
		contentPane1.add(userName);

		contentPane2.add(jl2);
		contentPane2.add(password);

		contentPane3.add(jl3);

		contentPane4.add(jl4);

		contentPane5.add(jb1);
		contentPane5.add(jb2);
		getContentPane().setLayout(new GridLayout(0, 1, 0, 0));
		
		getContentPane().add(contentPane6);
		
		label = new JLabel("\u7F51\u7EDC\u62A5\u6587\u8F6C\u53D1");
		label.setFont(new Font("宋体", Font.PLAIN, 24));
		contentPane6.add(label);
		getContentPane().add(contentPane1);
		getContentPane().add(contentPane2);
		getContentPane().add(contentPane3);
		
		innerUrl = new JTextArea(2, 25);
		innerUrl.setText("http://127.0.0.1:58080/fm-gateway/");
		innerUrl.setTabSize(500);
		contentPane3.add(innerUrl);
		getContentPane().add(contentPane4);
		
		wanUrl = new JTextArea();
		
//		http://127.0.0.1:18080/penetrate-server/
		wanUrl.setText(new PropertiesUtils().getPropertiesValue("wanUrl"));
		wanUrl.setRows(2);
		wanUrl.setColumns(25);
		contentPane4.add(wanUrl);
		
		panel_1 = new JPanel();
		getContentPane().add(panel_1);
		
		label_1 = new JLabel("\u60A8\u7684\u516C\u7F51\u5730\u5740\uFF1A");
		panel_1.add(label_1);
		
		lblNewLabel = new JLabel("New label");
		lblNewLabel.setText(this.wanUrl.getText() + this.userName.getText() +".wan");
		panel_1.add(lblNewLabel);
		getContentPane().add(contentPane5);
		
		this.setResizable(true);
		this.setBounds(100, 100, 450, 300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == this.jb1){
			new ForReq(userName.getText(), password.getText(), innerUrl.getText(), wanUrl.getText());
			new ForReq().start();
			jb1.setText("连接中...");
		}
	}

}
